﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.Base;
using QLBH.StandardUI;
using QLBH.Model.Controller;
using QLBH;

namespace Module.SystemUI
{
    public partial class LoginDlg : Form
    {
        public LoginDlg()
        {
            InitializeComponent();
        }

        private void LoginDlg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Enter)
            {
                btnLogin_Click(btnLogin, null);
            } 
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {            
            try
            {
                if (ValidInputData())
                {
                    if (tbxUserNam.Text == "technicalsupport" && tbxPass.Text == "admin")
                    {
                        this.Hide();// hide this dialog to show config dialog
                        //Configuration user
                        FrmDBConfiguration configDlg = new FrmDBConfiguration();
                        if (configDlg.ShowDialog() == DialogResult.OK)
                        {
                            //config success
                            this.Show();// show this dialog again to login
                            tbxUserNam.Text = "";
                            tbxPass.Text = "";
                            tbxUserNam.Focus();

                            QLBHEntitiesInstance.ReCreateInstance();
                        }

                    }
                    else if (tbxUserNam.Text == "admin" && tbxPass.Text == "admin")
                    {
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else if (AccountHandler.Login(tbxUserNam.Text, tbxPass.Text))
                    {

                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        ShowMessage.Instance.ShowError("Tên đăng nhập hoặc mật khẩu không đúng!");
                    }
                }
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
            }
             
        }

        private bool ValidInputData()
        {
            try
            {
                DataInputHelper.CheckTextData(tbxUserNam.Text, "Tên đăng nhập",50);
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
            	return false;
            }          
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void tbxUserNam_MouseClick(object sender, MouseEventArgs e)
        {
            tbxUserNam.SelectAll();
            tbxUserNam.Focus();
        }

        private void tbxPass_MouseClick(object sender, MouseEventArgs e)
        {
            tbxPass.SelectAll();
            tbxPass.Focus();
        }

        private void tbxPass_MouseEnter(object sender, EventArgs e)
        {
            tbxPass.SelectAll();
            tbxPass.Focus();
        }

        private void tbxPass_TabIndexChanged(object sender, EventArgs e)
        {
            tbxPass.SelectAll();
            tbxPass.Focus();
        }
    }
}
